package com.example.wordfilter.service;

import com.example.wordfilter.properties.WordReaderProperties;
import com.example.wordfilter.util.WordReaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

@EnableConfigurationProperties({WordReaderProperties.class})
public class WordFilter {
    @Autowired
    private WordReaderProperties readerProperties;
    private final Logger logger = Logger.getLogger(WordFilter.class.getName());

    @Bean
    @ConditionalOnMissingBean
    public WordBean getSensitiveWord(){
        boolean bool = readerProperties.getCensorWordEnable();
        WordBean sensitiveWordBean = new WordBean();
        if(bool){
            if(readerProperties.getIteratorEnabled()){
                Set<String> set = WordReaderUtil.toSet(readerProperties.getCensorWordsLocation());
                sensitiveWordBean.setSet(set);
                logger.log(Level.INFO,"加载敏感词到set，文件加载路径：" + readerProperties.getCensorWordsLocation());
            }
        }
        return sensitiveWordBean;
    }
}
